package cn.mifengkong.appcommon.webview;

/**
 * Created by jiangyongxing on 2017/5/19.
 * 描述：
 */

public class JsLocalStorageConstantUtil {
    /**
     * 将一些需要的值设置到localStorage中  以供前端开发使用
     *
     * @param userId
     * @param token
     * @param phoneNumber
     * @param platVersion
     * @param platType
     * @param packageName
     * @param sourceId
     * @param appUnionName
     * @return
     */
    public static String setDataToLocalStorageConstant(String userId, String token, String phoneNumber,
                                                       String platVersion, String platType, String packageName,
                                                       String sourceId, String appUnionName, String cityId) {
        String js = "window.localStorage.setItem('userId','" + userId + "');" +
                "window.localStorage.setItem('token','" + token + "');" +
                "window.localStorage.setItem('phoneNumber','" + phoneNumber + "');" +
                "window.localStorage.setItem('plat_version','" + platVersion + "');" +
                "window.localStorage.setItem('plat_type','" + platType + "');" +
                "window.localStorage.setItem('package_name','" + packageName + "');" +
                "window.localStorage.setItem('source_id','" + sourceId + "');" +
                "window.localStorage.setItem('city_id','" + cityId + "');" +
                "window.localStorage.setItem('app_union_name','" + appUnionName + "');";
//
        String jsstr = "javascript:(function(){" + js + "})()";

        return jsstr;
    }

    /**
     * 清除localStorage中的数据
     */
    public static String clearLocalStorageData() {
        String jsstr = "javascript:(function(){window.localStorage.clear();})()";
        return jsstr;
    }

    public static String getUnionpayOrderNumber() {
        String js =
                "var objs = document.getElementById('btnGetCode'); " +
                        "objs.ontouchend = function()" +
                        "    { " +
                        "var order_num = $('#order_num').attr('title');" +
//                        "var order_num = '98989'" +
                        "window.onCallByH5.getCashOrderNumber(order_num);" +
                        "    }";
        return jsFunctionShell(js);
    }

    public static String jsFunctionShell(String js) {
        return "javascript:(function(){" + js + "})()";
    }

    public static String getCashFailureMessageJs(String url) {
        return jsFunctionShell("setTimeout(function () {\n" +
                "            var payDom = document.getElementsByClassName('pay_img_div')[0];\n" +
                "            window.onCallByH5.getCashFailureMessage(payDom.innerText,'" + url + "')\n" +
                "        }, 1500)");
    }

    public static String getCommonJsBridgeInitCode(String otherJs) {
        return jsFunctionShell("try{var connectWebViewJavascriptBridge=function(callback){if(window.WebViewJavascriptBridge){callback(WebViewJavascriptBridge)}else{document.addEventListener(\"WebViewJavascriptBridgeReady\",function(){callback(WebViewJavascriptBridge)},false)}};try{connectWebViewJavascriptBridge(function(bridge){bridge.init(function(message,responseCallback){var data={\"Javascript Responds\":\"Wee!\"};responseCallback(data)})})}catch(e){}var setupWebViewJavascriptBridge=function(callback){if(window.WebViewJavascriptBridge){return callback(WebViewJavascriptBridge)}if(window.WVJBCallbacks){return window.WVJBCallbacks.push(callback)}window.WVJBCallbacks=[callback];var WVJBIframe=document.createElement(\"iframe\");WVJBIframe.style.display=\"none\";WVJBIframe.src=\"https://__bridge_loaded__\";document.documentElement.appendChild(WVJBIframe);setTimeout(function(){document.documentElement.removeChild(WVJBIframe)},0)};var frBridge=function(api,parameter,callback){setupWebViewJavascriptBridge(function(bridge){bridge.callHandler(api,parameter,callback)})};" +
                otherJs +
                "}catch(e){};");
    }

}
